(function() {
    'use strict';

    /**
    公众号已关联的app
     */
    angular.module('webApp')
        .directive('gzhLinkedApp', ['dataService', '$compile', 'helper', '$timeout',
            function(dataService, $compile, helper, $timeout) {
                return {
                    restrict: 'EA',
                    templateUrl: "views/directives.gzh.authrizedapp.html",
                    require: 'ngModel',
                    scope: {
                        ngModel: '=',
                        gzhAppId: "@", //公众号appid
                        storeId: "@", //大黄蜂门店id
                        shopId: "@", //门店id
                    },
                    controller: ['$templateCache', '$rootScope', '$scope', 'sessionService',
                        function($templateCache, $rootScope, $scope, sessionService) {
                            $scope.authrizedApp = [];
                            //获取公众号关联的小程序
                            $scope.initData = function() {
                                dataService.gzhWxopenWxamplink($scope.gzhAppId).then(function(a) {
                                    $scope.attachedapps = a;
                                    allAuthrizeAppGet(true).then(function() {
                                        //已授权的名字数组
                                        var authrizedName = _.pluck($scope.attachedapps, "nickname");
                                        //授权门店的大黄蜂小程序
                                        var storeAppName = _.pluck($scope.storeApps, "nick_name");
                                        //已授权的小程序
                                        $scope.authrizedApp = _.intersection(authrizedName, storeAppName);
                                    });
                                });
                            }
                            $scope.ifGzhAuthrize = function(name) {
                                return $scope.authrizedApp.indexOf(name) != -1 ? true : false;
                            }
                            //保存主页
                            $scope.saveHome = function() {
                                if (!$scope.ngModel) {
                                    return helper.myAlert("没有选择小程序");
                                }
                                dataService.gzhWifiFinishPageSet($scope.gzhAppId, $scope.shopId, $scope.ngModel).then(function() {
                                    dataService.gzhWifiHomePageSet($scope.gzhAppId, $scope.shopId, $scope.ngModel).then(function() {
                                        helper.myAlert("设置成功！");
                                    });
                                });
                            }

                            //所有授权进来的小程序
                            function allAuthrizeAppGet(nocache) {
                                return dataService.sellerAuthorizeAuthorizer_infoGet($scope.storeId, 2, nocache).then(function(result) {
                                    $scope.storeApps = result;
                                });
                            }
                        }
                    ],

                    link: function(scope, element, attrs, ctrl) {
                        // scope.$watch('appId', function(newValue, oldValue, scope) {
                        //     if (newValue) {
                        //         scope.shopGet();
                        //     }
                        // });

                        scope.$on('event:up-store-id', function() {
                            scope.initData();
                        });

                        ctrl.$render = function() {
                            scope.initData();

                            // if (ctrl.$viewValue && !scope.type) {
                            //     ctrl.$setViewValue(ctrl.$viewValue);
                            // }
                            // if (scope.type) {
                            //     scope.ngModel = scope.ngModel ? scope.ngModel : [];
                            // }
                        };


                    }
                }
            }
        ])
        .directive('gzhShopList', ['dataService', '$compile', 'helper', '$timeout',
            function(dataService, $compile, helper, $timeout) {
                return {
                    restrict: 'EA',
                    templateUrl: "views/directives.gzh.app.html",
                    require: 'ngModel',
                    scope: {
                        ngModel: '=',
                        dft: "@", //m默认值
                        stype: "@", //只显示值
                        appId: "@",
                        callbacks: "&",
                    },
                    controller: ['$templateCache', '$rootScope', '$scope', 'sessionService',
                        function($templateCache, $rootScope, $scope, sessionService) {
                            $scope.change = function(newValueid) {
                                if (newValueid) {
                                    $scope.ngModel = newValueid;
                                    _.map($scope.authorlist, function(isd, ik) {
                                        if (isd.app_id == newValueid) {
                                            $scope.ary = isd;
                                        }
                                    });

                                    if ($scope.callbacks) {
                                        // console.error("newValueid", newValueid);
                                        $scope.callbacks({ app_id: newValueid });
                                    }
                                }
                            }

                            //店铺查询
                            $scope.shopGet = function() {
                                return dataService.gzhShopList($scope.appId).then(function(a) {
                                    console.error("aaaaaa", a)
                                    $timeout(function() {
                                        $scope.shops = a;
                                    });
                                });
                            }
                        }
                    ],

                    link: function(scope, element, attrs, ctrl) {
                        scope.$watch('appId', function(newValue, oldValue, scope) {
                            if (newValue) {
                                scope.shopGet();
                            }
                        });

                        ctrl.$render = function() {
                            if (ctrl.$viewValue && !scope.type) {
                                ctrl.$setViewValue(ctrl.$viewValue);
                            }
                            if (scope.type) {
                                scope.ngModel = scope.ngModel ? scope.ngModel : [];
                            }
                        };


                    }
                }
            }
        ])
        .run(["$templateCache", "dataService",
            function($templateCache, dataService) {
                var tplName = "views/directives.gzhsucailist.html";
                dataService.tpl(tplName).then(function(html) {
                    $templateCache.put(tplName, html);
                });
            }
        ])
        .directive('getGzhSucaiList', ['dataService', '$compile', 'helper', '$timeout',
            function(dataService, $compile, helper, $timeout) {
                return {
                    restrict: 'EA',
                    template: '<div><a href="javascript:void(0);" ng-click="change()" ><span ng-show="!type"> 从公众号图文消息中选择</span><span ng-show="type"><i style="margin-left: 32px;" class="sui-icon icon-list"></i><br/><strong>从素材库选择</strong></span></a></div>',
                    scope: {
                        wxappid: '=',
                        callbacks: "&",
                        type:"@"
                    },
                    controller: ['$templateCache', '$rootScope', '$scope', 'sessionService',
                        function($templateCache, $rootScope, $scope, sessionService) {
                            $scope.change = function(pageNo) {
                                //查询数据、
                                $scope.getlist(1).then(showPop);
                            }

                            $scope.getlist = function(pageNo) {
                                console.error($scope.wxappid);
                                if ($scope.wxappid) {
                                    console.error(pageNo);
                                    $scope.tabtype = 'news'; //默认
                                    console.error(pageNo);
                                    // var offset = pageNo > 1 ? pageNo + 1 : pageNo;
                                    $scope.address = {
                                        "type": $scope.tabtype,
                                        "offset": pageNo,
                                        "count": $scope.pageSize ? $scope.pageSize : 10,
                                    }

                                    return dataService.wxproxy($scope.wxappid, 'https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=TOKEN', 'POST', $scope.address).then(function(result) {
                                        console.error("result", result);
                                        $scope.itemlist = result.item;
                                        $scope.total_count = result.total_count;
                                        $scope.item_count = result.item_count;

                                    });
                                }

                            }

                            function showPop() {
                                $.alert({
                                    backdrop: 'static',
                                    keyboard: true,
                                    title: '{{"选择图文消息" | translate}}',
                                    body: $templateCache.get("views/directives.gzhsucailist.html"),
                                    okBtn: '{{"ok" | translate}}',
                                    closeBtn: true,
                                    transition: true,
                                    width: 'large',
                                    height: 400,
                                    show: function(e) {
                                        $compile($(e.currentTarget).contents())($scope);
                                        return true;
                                    },
                                    okHide: function(e) {
                                        $timeout(function() {
                                            if ($scope.checkdata && $scope.callbacks) {
                                                $scope.callbacks({data: $scope.checkdata});
                                            }
                                        });
                                    },
                                });
                            }
                            //
                            $scope.clickData =function(data){
                                console.error("uel",data);
                                if(data){
                                    $scope.checkdata = data;
                                }
                            }

                        }
                    ],

                    link: function(scope, element, attrs, ctrl) {


                    }
                }
            }
        ]);
})();